package com.tongtong.stsuserapi.controller;

import com.tongtong.stscommon.enumration.OrderState;
import com.tongtong.stscommon.message.STSMessage;
import com.tongtong.stscommon.proxy.annotation.AutoGetId;
import com.tongtong.stsuserapi.pojo.Item;
import com.tongtong.stsuserapi.pojo.search.ItemSearch;
import com.tongtong.stsuserapi.service.base.Impl.ItemServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Objects;

/**
 * 商品控制器
 */
@Api(tags="商品接口")
@RestController
@RequestMapping("/sts/item")
public class ItemController {

	@Autowired
	ItemServiceImpl itemService;

	@ApiOperation("发布一个商品")
	@ApiImplicitParam(name = "id", value = "该参数从token中自动获取，无需传入")
	@PostMapping
	@AutoGetId
	@PreAuthorize("hasAuthority('user')")
	public STSMessage postItem(Long id, @RequestBody Item item) {
			return itemService.postItem(id,item);
	}

	@ApiOperation("更换商品图片")
	@PostMapping("/uploadIMG")
	@PreAuthorize("hasAuthority('user')")
	public STSMessage uploadIMGById(Long itemId, @RequestPart("file") MultipartFile file) throws Exception {
		return itemService.uploadIMGById(itemId,file);
	}

	@ApiOperation("获取商品详情")
	@GetMapping
	public STSMessage getItemInfoById(Long itemId) {
		return itemService.getItemInfoById(itemId);
	}

	@ApiOperation("搜索商品")
	@PostMapping("/search")
	public STSMessage dynamicSearchItem(@RequestBody ItemSearch itemSearch, int page, int pageSize) {
		return itemService.searchItemByPage(itemSearch,page,pageSize);
	}

	@ApiOperation("更新商品信息")
	@PatchMapping
	@PreAuthorize("hasAuthority('user')")
	public STSMessage updateItemById(@RequestBody Item item) {
		return itemService.updateItemById(item);
	}

	@ApiOperation("删除发布的商品")
	@DeleteMapping
	@PreAuthorize("hasAuthority('user')")
	public STSMessage deleteItemById(Long id) {
		return itemService.deleteItemById(id);
	}

	@ApiOperation("获取已经发布的商品")
	@ApiImplicitParam(name = "userid", value = "该参数从token中自动获取，无需传入")
	@GetMapping("/published")
	@AutoGetId
	STSMessage getPublishedItems(Long userid) {
		return itemService.getPublishedItems(userid);
	}

	@ApiOperation("获取已被预定的商品")
	@ApiImplicitParam(name = "userid", value = "该参数从token中自动获取，无需传入")
	@GetMapping("/booked")
	@AutoGetId
	STSMessage getBookedItems(Long userid) {
		return itemService.getBookedOrSoldItems(userid, OrderState.BOOKED);
	}

	@ApiOperation("获取已售出的商品")
	@ApiImplicitParam(name = "userid", value = "该参数从token中自动获取，无需传入")
	@GetMapping("/sold")
	@AutoGetId
	STSMessage getSoldItems(Long userid) {
		return itemService.getBookedOrSoldItems(userid, OrderState.COMPLETED);
	}

}
